﻿using System.Collections.Generic;
using System.Modules.SharePoint.Contracts.Enums;
using System.Modules.Contracts.DTO;
using System.Text;
using System.Modules.Extensions;
using System.Modules.SharePoint.Constants;

namespace System.Modules.SharePoint.Contracts.DTO
{
    /// <summary>
    /// SPSiteDataQueryLists
    /// </summary>
    public class SPSiteDataQueryLists : Initiator
    {
        #region Properties
        /// <summary>
        /// Limits the query to lists of the specified server template. By default, this attribute is not set and so the query is not limited to lists based on a particular template.
        /// </summary>
        public SPListServerTemplate? ServerTemplate { get; set; }

        /// <summary>
        /// Limits the query to lists of the specified base type. By default, the query considers lists of BaseType 0 (generic lists)
        /// </summary>
        public SPListBaseType? BaseType { get; set; }

        /// <summary>
        /// Determines whether the query includes hidden lists. By default, the query considers all non-hidden lists.
        /// </summary>
        public bool? Hidden { get; set; }

        /// <summary>
        /// Limits the query to the total number of lists specified. If the query would exceed the limit, the query instead fails and raises an SPException. By default, the limit is 1000. When set to 0, there is no limit to the number of lists that are considered.
        /// </summary>
        public int? MaxListLimit { get; set; }

        /// <summary>
        /// Lists Subelements
        /// </summary>
        public List<SPSiteDataQueryListsSubElement> SubElements { get; set; }
        #endregion

        #region Implicit Operators
        /// <summary>
        /// Implicit conversion operator to string
        /// </summary>
        /// <param name="value">The SP Query Value</param>
        /// <returns>A string representation of the SP Site Data Query Lists object</returns>
        public static implicit operator string(SPSiteDataQueryLists value)
        {
            return value == null ? string.Empty : value.ToString();
        }
        #endregion

        #region Methods
        public override string ToString()
        {
            var value = new StringBuilder();
            //Build attributes
            if (ServerTemplate.HasValue)
            {
                value.AppendFormat(SPQueryTemplateAttributes.SERVER_TEMPLATE, ServerTemplate.Value.Value());
            }
            if (BaseType.HasValue)
            {
                value.AppendFormat(SPQueryTemplateAttributes.BASE_TYPE, BaseType.Value.Value());
            }
            if (Hidden.HasValue)
            {
                value.AppendFormat(SPQueryTemplateAttributes.HIDDEN, Hidden.Value);
            }
            if (MaxListLimit.HasValue)
            {
                value.AppendFormat(SPQueryTemplateAttributes.MAX_LIST_LIMIT, MaxListLimit.Value);
            }
            var attributes = value.ToString();
            value = new StringBuilder();
            //Build Lists XML if Any
            if (SubElements != null)
            {
                foreach (var element in SubElements)
                {
                    value.Append(element);
                }
            }
            var lists = value.ToString();
            value = new StringBuilder();
            value.AppendFormat(SPQueryTemplates.SP_SITE_DATA_QUERY_LISTS, attributes, lists);
            return value.ToString();
        }
        #endregion
    }
}
